package com.loc;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;
import com.loc.bt;
import com.sangfor.ssl.service.utils.IGeneral;
import java.net.URL;
import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/* compiled from: AMapDnsManager.java */
/* loaded from: classes2.dex */
public final class fm {
    public static int a = 1;
    public static int b = 2;
    private static fm e;
    private Context j;
    private String k;
    private long c = 0;
    private boolean d = false;
    private ArrayList<String> f = new ArrayList<>();
    private et g = new et();
    private et h = new et();
    private long i = 120000;
    private boolean l = false;

    private fm(Context context) {
        this.j = context;
    }

    public static synchronized fm a(Context context) {
        fm fmVar;
        synchronized (fm.class) {
            if (e == null) {
                e = new fm(context);
            }
            fmVar = e;
        }
        return fmVar;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public et b(int i) {
        return i == b ? this.h : this.g;
    }

    private synchronized void b(boolean z, final int i) {
        if (!z) {
            if (!fs.p() && this.l) {
                return;
            }
        }
        if (this.c != 0) {
            long currentTimeMillis = System.currentTimeMillis();
            long j = this.c;
            if (currentTimeMillis - j < this.i) {
                return;
            }
            if (currentTimeMillis - j < 60000) {
                return;
            }
        }
        this.c = System.currentTimeMillis();
        this.l = true;
        StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
        StringBuffer stringBuffer = new StringBuffer();
        for (StackTraceElement stackTraceElement : stackTrace) {
            stringBuffer.append(stackTraceElement.getClassName() + "(" + stackTraceElement.getMethodName() + ":" + stackTraceElement.getLineNumber() + "),");
        }
        cr.a().b(new cs() { // from class: com.loc.fm.1
            @Override // com.loc.cs
            public final void a() {
                int i2;
                StringBuilder sb = new StringBuilder(IGeneral.PROTO_HTTP_HEAD);
                sb.append(fs.r());
                sb.append("?host=dualstack-a.apilocate.amap.com&query=");
                sb.append(i == fm.b ? 6 : 4);
                String sb2 = sb.toString();
                fn fnVar = new fn();
                fnVar.b(sb2);
                fnVar.c(sb2);
                fnVar.a(bt.a.SINGLE);
                fnVar.a(bt.c.HTTP);
                try {
                    bo.a();
                    JSONObject jSONObject = new JSONObject(new String(bo.a(fnVar).a));
                    String[] b2 = fm.b(jSONObject.optJSONArray("ips"), fm.a);
                    if (b2 != null && b2.length > 0 && !fm.b(b2, fm.this.b(fm.a).a())) {
                        fm.this.b(fm.a).a(b2);
                        fm.this.f(fm.a);
                    }
                    String[] b3 = fm.b(jSONObject.optJSONArray("ipsv6"), fm.b);
                    if (b3 != null && b3.length > 0 && !fm.b(b3, fm.this.b(fm.b).a())) {
                        fm.this.b(fm.b).a(b3);
                        fm.this.f(fm.b);
                    }
                    if ((jSONObject.has("ips") || jSONObject.has("ipsv6")) && jSONObject.has("ttl") && (i2 = jSONObject.getInt("ttl")) > 30) {
                        fm.this.i = i2 * 1000;
                    }
                } catch (Throwable th) {
                    JSONObject jSONObject2 = new JSONObject();
                    try {
                        jSONObject2.put("key", "dnsError");
                        jSONObject2.put("reason", th.getMessage());
                    } catch (Throwable unused) {
                    }
                    fz.a(fm.this.j, "O018", jSONObject2);
                }
            }
        });
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static boolean b(String[] strArr, String[] strArr2) {
        if (strArr == null || strArr.length == 0 || strArr2 == null || strArr2.length == 0 || strArr.length != strArr2.length) {
            return false;
        }
        int length = strArr.length;
        for (int i = 0; i < length; i++) {
            if (!strArr[i].equals(strArr2[i])) {
                return false;
            }
        }
        return true;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static String[] b(JSONArray jSONArray, int i) throws JSONException {
        if (jSONArray == null || jSONArray.length() == 0) {
            return new String[0];
        }
        int length = jSONArray.length();
        String[] strArr = new String[length];
        for (int i2 = 0; i2 < length; i2++) {
            String string = jSONArray.getString(i2);
            if (!TextUtils.isEmpty(string)) {
                if (i == b) {
                    string = "[" + string + "]";
                }
                strArr[i2] = string;
            }
        }
        return strArr;
    }

    private static String c(int i) {
        return i == b ? "last_ip_6" : "last_ip_4";
    }

    private void d(int i) {
        if (b(i).d()) {
            SharedPreferences.Editor a2 = ga.a(this.j, "cbG9jaXA");
            ga.a(a2, c(i));
            ga.a(a2);
            b(i).a(false);
        }
    }

    private String e(int i) {
        String str;
        int i2 = 0;
        b(false, i);
        String[] a2 = b(i).a();
        if (a2 == null || a2.length <= 0) {
            g(i);
            return b(i).b();
        }
        int length = a2.length;
        while (true) {
            if (i2 >= length) {
                str = null;
                break;
            }
            str = a2[i2];
            if (!this.f.contains(str)) {
                break;
            }
            i2++;
        }
        if (TextUtils.isEmpty(str)) {
            return null;
        }
        b(i).a(str);
        return str;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void f(int i) {
        if (b(i).a() == null || b(i).a().length <= 0) {
            return;
        }
        String str = b(i).a()[0];
        if (str.equals(this.k) || this.f.contains(str)) {
            return;
        }
        this.k = str;
        SharedPreferences.Editor a2 = ga.a(this.j, "cbG9jaXA");
        ga.a(a2, c(i), str);
        ga.a(a2);
    }

    private void g(int i) {
        String a2 = ga.a(this.j, "cbG9jaXA", c(i), (String) null);
        if (TextUtils.isEmpty(a2) || this.f.contains(a2)) {
            return;
        }
        b(i).a(a2);
        b(i).b(a2);
        b(i).a(true);
    }

    public final String a(fp fpVar, int i) {
        try {
            if (fs.q() && fpVar != null) {
                String b2 = fpVar.b();
                String host = new URL(b2).getHost();
                if (!"http://abroad.apilocate.amap.com/mobile/binary".equals(b2) && !"abroad.apilocate.amap.com".equals(host)) {
                    String str = "apilocate.amap.com".equalsIgnoreCase(host) ? "httpdns.apilocate.amap.com" : host;
                    if (!m.g(str)) {
                        return null;
                    }
                    String e2 = e(i);
                    if (!TextUtils.isEmpty(e2)) {
                        fpVar.d(b2.replace(host, e2));
                        fpVar.a().put("host", str);
                        fpVar.e(str);
                        fpVar.a(i == b);
                        return e2;
                    }
                }
            }
        } catch (Throwable unused) {
        }
        return null;
    }

    public final void a(int i) {
        if (b(i).e()) {
            d(i);
            return;
        }
        this.f.add(b(i).b());
        d(i);
        b(true, i);
    }

    public final void a(boolean z, int i) {
        b(i).b(z);
        if (z) {
            String c = b(i).c();
            String b2 = b(i).b();
            if (TextUtils.isEmpty(b2) || b2.equals(c)) {
                return;
            }
            SharedPreferences.Editor a2 = ga.a(this.j, "cbG9jaXA");
            ga.a(a2, c(i), b2);
            ga.a(a2);
        }
    }
}
